package com.proton.carepatchtemp.fragment.dialogfragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.activity.HealthTipsActivity;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.constant.Settings;
import com.proton.carepatchtemp.utils.SpUtils;

import java.util.Locale;

public class UseNoticeFragment extends DialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private View view;
    /**
     * 我知道了
     */
    private Button mBtCloseUseNotice;

    private boolean isShow = true;
    /**
     * 下次不再提示
     */
    private TextView mTvIsShowUse;
    private ImageView mIvUseNotice;
    /**
     * 点击观看卡帕奇体温贴操作视频
     */
    private TextView mTvWatchTv;

    public UseNoticeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment UseNoticeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static UseNoticeFragment newInstance(String param1, String param2) {
        UseNoticeFragment fragment = new UseNoticeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.CENTER); //可设置dialog的位置
        window.getDecorView().setPadding(64, 0, 64, 0); //消除边距

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        window.setAttributes(lp);

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_use_notice, container, false);
        initView(view);

        mTvWatchTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        mTvWatchTv.setOnClickListener(v -> {
            Settings.isPlayerTv = true;
            Intent goHealthTips = new Intent(getContext(), HealthTipsActivity.class);
            goHealthTips.putExtra(Extras.MSG_HEALTH_TIPS_URL, "http://www.protontek.com/tempIntroduce/index.html");
            goHealthTips.putExtra(Extras.WEB_TITLE, getString(R.string.string_video));
            startActivity(goHealthTips);
        });

        if (isLunarSetting()) {

        } else {
            mIvUseNotice.setImageResource(R.mipmap.realtime_use_notice_img_eg);
        }

        mTvIsShowUse.setOnClickListener(v -> {
            if (isShow) {
                isShow = false;
                mTvIsShowUse.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(VCare.get(), R.mipmap.check_square), null, null, null);
            } else {
                isShow = true;
                mTvIsShowUse.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(VCare.get(), R.mipmap.uncheck_square), null, null, null);
            }
        });

        mBtCloseUseNotice.setOnClickListener(v -> {
            SpUtils.saveBoolean(Extras.IS_SHOW_USE_METHOD, isShow);
            UseNoticeFragment.this.dismiss();
        });
        return view;
    }

    public boolean isLunarSetting() {
        String language = getLanguageEnv();

        return language != null
                && (language.trim().equals("zh-CN") || language.trim().equals("zh-TW"));
    }

    private String getLanguageEnv() {
        Locale l = Locale.getDefault();
        String language = l.getLanguage();
        String country = l.getCountry().toLowerCase();
        if ("zh".equals(language)) {
            if ("cn".equals(country)) {
                language = "zh-CN";
            } else if ("tw".equals(country)) {
                language = "zh-TW";
            }
        } else if ("pt".equals(language)) {
            if ("br".equals(country)) {
                language = "pt-BR";
            } else if ("pt".equals(country)) {
                language = "pt-PT";
            }
        }
        return language;
    }

    private void initView(View view) {
        mBtCloseUseNotice = view.findViewById(R.id.bt_close_use_notice);
        mTvIsShowUse = view.findViewById(R.id.tv_is_show_use);
        mIvUseNotice = view.findViewById(R.id.iv_use_notice);
        mTvWatchTv = view.findViewById(R.id.tv_watch_tv);
    }

}
